// pages/user/account/withdraw/withdraw.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        accountInfo: {},
        aliAccountNum: '',
        aliAccountName: '',
        withdrawMoney: null,
        showCardSelect: false,
        isLoading: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getAccountInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getAliAccount()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getAccountInfo: function() {
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let data = {}
        common.post('Wallet/user_money', data).then(function(res) {
            wx.hideLoading()
            that.setData({
                accountInfo: res
            })
        }).catch(function(e) {})
    },

    getAliAccount: function () {
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let data = {}
        common.post('User/user_info', data).then(function (res) {
            wx.hideLoading()
            if (res.alipay_sn && res.real_name) {
                that.setData({
                    aliAccountNum: res.alipay_sn,
                    aliAccountName: res.real_name
                })
            }
        }).catch(function (e) { })
    },

    bindReplaceInput: function(e) {
        this.setData({
            withdrawMoney: e.detail.value
        })
    },

    selectCard: function() {
        this.setData({
            showCardSelect: true
        })
    },

    closeSelectCard: function() {
        this.setData({
            showCardSelect: false
        })
    },

    myCatchTouch: function() {
        return
    },

    selectAliAccount: function () {
        wx.navigateTo({
            url: '/pages/user/aliAccount/add/add'
        })
    },

    confirmWithdraw: function() {
        if (this.data.isLoading) {
            wx.showToast({
                title: '请勿重复操作',
                icon: 'none',
                duration: 1000
            })
            return
        }
        if (!this.data.aliAccountNum && !this.data.aliAccountName) {
            wx.showToast({
                title: '请添加支付宝账户',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.withdrawMoney == null || this.data.withdrawMoney == '') {
            wx.showToast({
                title: '请输入提现金额',
                icon: 'none',
                duration: 1000
            })
            return
        }
        if (this.data.withdrawMoney % 100 != 0) {
            wx.showToast({
                title: '提现金额必须为整百',
                icon: 'none',
                duration: 1000
            })
            return
        }
        wx.showLoading({
            title: '加载中',
        })
        this.setData({
            isLoading: true
        })
        const that = this
        wx.login({
            success(res) {
                if (res.code) {
                    //发起网络请求
                    let data = {
                        money: that.data.withdrawMoney,
                        code: res.code
                    }
                    common.post('Withdraw/apply_assist_get', data).then(function(res) {
                        that.setData({
                            isLoading: false
                        })
                        wx.hideLoading()
                        if (res.code == 204) {
                            wx.showModal({
                                title: '提示',
                                content: res.error,
                                showCancel: false,
                                success(res) {
                                    if (res.confirm) {
                                        wx.navigateBack({
                                            delta: 1
                                        })
                                    }
                                }
                            })
                        } else {
                            wx.showModal({
                                title: '提示',
                                content: res,
                                showCancel: false,
                                success(res) {
                                    if (res.confirm) {
                                        wx.navigateBack({
                                            delta: 1
                                        })
                                    }
                                }
                            })
                        }
                    }).catch(function(e) {})
                }
            }
        })
    }
})